//
//  HomePageViewController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/12.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "HomePageViewController.h"
#import "SDCycleScrollView.h"
#import "LoginViewController.h"
@interface HomePageViewController ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSDictionary *dict;
@end

@implementation HomePageViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        
        self.tiyan.layer.shadowOpacity = 1;
        self.tiyan.layer.cornerRadius = self.tiyan.height/2;
      
    });
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self setupViewsTouch];
    [self requestData];
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05/*延迟执行时间*/ * NSEC_PER_SEC));

    dispatch_after(delayTime, dispatch_get_main_queue(), ^{

      
            self.tiyan.layer.cornerRadius = self.tiyan.height/2;
     
    });

    // Do any additional setup after loading the view.
}

- (void)requestData {
    _array = [NSMutableArray array];
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1000",API_LOGIN_URL_STRING];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
    
        _dict = dict;
        for (NSDictionary *dic in dict[@"bannerList"][@"banner"]) {

            [self.array addObject:dic[@"img"]];
        }

        [self setUpScrollView];
    } failure:^(NSError *error) {
        
    } showHUD:NO];
    
    
    NSString *url = [NSString stringWithFormat:@"%@OPT=1039&sign=%@",API_LOGIN_URL_STRING,USER_SIGN];
    [[CDHttpHelper defaultCDHttpHelper] get:url params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        
        NSLog(@"%@",dict);
        [self.tiyan setTitle:dict[@"btnText"] forState:UIControlStateNormal];
        self.rate.text = dict[@"rate"];
        self.money.text = [NSString stringWithFormat:@"赠送金额：%@",dict[@"money"]];
        self.term.text = [NSString stringWithFormat:@"期限：%@",dict[@"term"]];
    } failure:^(NSError *error) {
        
    } showHUD:NO];
}


- (void)setupViews {
    
   
    _guide.adjustsImageWhenHighlighted = NO;
    
    _activity.adjustsImageWhenHighlighted = NO;
   
    _explain.adjustsImageWhenHighlighted = NO;
    
    _assessment.adjustsImageWhenHighlighted = NO;
  
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"赠送2999元体验金及200元红包"];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 5)];

    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(11, 4)];
    
    self.tyj.attributedText = string;
    
    self.navigationItem.backBarButtonItem = BarButtonItem;
   // CGFloat height = _tiyan.frame.size.height;
    
}
//第三方轮播图
- (void)setUpScrollView {

    SDCycleScrollView *cycleScrollView3 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*2.8/10-12) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView3.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    cycleScrollView3.currentPageDotColor = [UIColor colorWithHex:@"#118cf8"];
    cycleScrollView3.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    cycleScrollView3.imageURLStringsGroup = _array;
    NSLog(@"%@",_array);
    
    [self.topView addSubview:cycleScrollView3];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
}


//添加手势
- (void)setupViewsTouch {
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_leftView addGestureRecognizer:tapGesturRecognizer];
    [_rightView addGestureRecognizer:tapGesturRecognizer];
    [_centerView addGestureRecognizer:tapGesturRecognizer];
}

-(void)tapAction:(id)tap {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"HomePage" bundle:[NSBundle mainBundle]];
    HomePageDetailController *hpdc = [board instantiateViewControllerWithIdentifier:@"hpdc"];
    hpdc.hidesBottomBarWhenPushed = YES;
    hpdc.titleStr = @"新手专享";
    hpdc.filepath = [NSURL URLWithString:_dict[@"xrzx"][@"img"]];
    [self showViewController:hpdc sender:self];
    NSLog(@"1111");
}
- (IBAction)detail:(UIButton *)sender {
    NSLog(@"%@",_dict);
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"HomePage" bundle:[NSBundle mainBundle]];
    HomePageDetailController *hpdc = [board instantiateViewControllerWithIdentifier:@"hpdc"];
    UIStoryboard *board1 = [UIStoryboard storyboardWithName:@"My" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [board1 instantiateViewControllerWithIdentifier:@"evc"];
    NSString *path;
    NSString *title;
    switch (sender.tag) {
        case 100:
            title = @"新手引导";
            path = _dict[@"xsyd"][@"img"];
            break;
        case 101:
            title = @"安全说明";
            path = _dict[@"aqsm"][@"img"];
            break;
        case 102:
            title = @"邀请活动";
            path = _dict[@"yqhd"][@"img"];
            break;
        case 103:
            [self.navigationController pushViewController:vc animated:YES];
            vc.hidesBottomBarWhenPushed = YES;
            return;
            break;
        default:

            break;
    }
    hpdc.filepath = [NSURL URLWithString:path];
    hpdc.titleStr = title;
    hpdc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:hpdc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
